//
//  EmptyTableView.swift
//  pBox
//
//  Created by TianChenIMac1 on 2019/9/30.
//  Copyright © 2019 skymesh. All rights reserved.
//

import UIKit
import MJRefresh
import EmptyDataSet_Swift
public class EmptyTableView: UITableView {
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        self.emptyDataSetSource = self
        self.emptyDataSetDelegate = self
        self.tableViewFooter.setTitle("已经到底啦～", for: .noMoreData)
        self.mj_footer = self.tableViewFooter
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.emptyDataSetSource = self
        self.emptyDataSetDelegate = self
        
        self.tableViewFooter.setTitle("已经到底啦～", for: .noMoreData)
        self.mj_footer = self.tableViewFooter
    }
    var verticalOffset :CGFloat = 0
    
    public var didTapEmptyView : ((UIView) -> Void)?
    public var emptyTitle : (() -> NSAttributedString?)?
    public var emptyImage : (() -> UIImage?)?
    public var emptyButtonTitle : ((UIControl.State) -> NSAttributedString?)?
    public var emptyButtonImage : ((UIControl.State) -> UIImage?)?
    public var emptyButtonBackgroundImage : ((UIControl.State) -> UIImage?)?
    public var tableViewFooter = MJRefreshAutoNormalFooter()
    
    
    public override func reloadData() {
        
        super.reloadData()
        self.layoutIfNeeded()
        if self.mj_contentH >= self.mj_size.height{
            self.mj_footer?.isHidden = false
            self.mj_footer?.endRefreshingWithNoMoreData()
            
        }else{
            self.mj_footer?.isHidden = true
        }
    }
}


extension EmptyTableView : EmptyDataSetDelegate {
    public func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView) {
        didTapEmptyView?(view)
    }
    
    public func emptyDataSet(_ scrollView: UIScrollView, didTapButton button: UIButton) {
        didTapEmptyView?(button)
    }
}

extension EmptyTableView : EmptyDataSetSource {
    public func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        return emptyTitle?() ?? NSAttributedString.init(string: "暂无数据")
    }
    
    //返回图片偏移量
    public func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return verticalOffset
    }
    
    
    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return emptyImage?()
    }
    
    public func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
        return emptyButtonTitle?(state)
    }
    
    public func buttonImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> UIImage? {
        return emptyButtonImage?(state)
    }
    
    public func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> UIImage? {
        return emptyButtonBackgroundImage?(state)
    }
}

extension UITableView {
    
    func cell<CellType : UITableViewCell>(with cellType: CellType.Type, cellStyle: UITableViewCell.CellStyle = .default) -> CellType {
        let identifier = NSStringFromClass(cellType)
        var cell = self.dequeueReusableCell(withIdentifier: identifier)
        if (cell == nil) {
            cell = cellType.init(style: cellStyle, reuseIdentifier: identifier)
            cell?.selectionStyle = .none
        }
        return cell as! CellType
    }
}


extension UITableView :UITextDragDelegate {
    
}
